package com.pbdemo.logic.action;


import com.pbdemo.protos.Pb;
import xyz.noark.core.annotation.Controller;
import xyz.noark.core.annotation.controller.ExecThreadGroup;
import xyz.noark.core.annotation.controller.PacketMapping;
import xyz.noark.core.network.NetworkProtocol;
import xyz.noark.core.network.Session.State;
import xyz.noark.log.Logger;
import xyz.noark.log.LoggerFactory;
import xyz.noark.network.SocketSession;

@Controller(threadGroup = ExecThreadGroup.ModuleThreadGroup)
public class DemoAction {
    Logger logger = LoggerFactory.getLogger(getClass());

    @PacketMapping(opcode = 1001,state = State.CONNECTED)
    public NetworkProtocol LoginHandler(SocketSession session, Pb.ReqLogin req) {
        logger.info("收到客户端登录请求 pb={}",req.toString());
        Pb.RespLogin resp = Pb.RespLogin.newBuilder().
                setOpenid("test_openid").
                setRoleName("张小五").
                setRoleLv(100).
                setRoleExp(111111).build();

        logger.info("返回客户端登录信息：{}",resp.toString());

        String uid = req.getUsername();
        session.setUid(uid);
        session.setState(State.AUTHENTICATED);
        logger.info("login success. uid={}, ip={}", uid, session.getIp());

        return new NetworkProtocol(1001,resp);
    }


}
